innerapp.controller('user_manager_Ctrl', ['$scope', '$rootScope', '$http', function ($scope, $rootScope, $http) {
    $scope.carname = "用户管理";
    $scope.newUserName = "";
    $scope.newUserPassword = "";
    $scope.password = "";
    $scope.users = [];
    $scope.currentUser;
    $scope.loginUserName = $rootScope.user;
    $http.get("/Service/ums/getUsers/")
        .then(function (result) {
            $scope.users = result.data;
            if ($scope.users.length > 0) {
                for (var index = 0; index < $scope.users.length; index++) {
                    if ($scope.users[index].Name == "Administrator") {
                        $scope.users.splice(index, 1);
                        break;
                    }
                }
                for (var index = 0; index < $scope.users.length; index++) {
                    $scope.users[index].Password = "";
                }
                $scope.toggle($scope.users[0]);
            }
        });
    $scope.toggle = function (user) {
        $scope.currentUser = user;
        $scope.password = "";
    };
    $scope.addUser = function () {
        $http.get("/Service/ums/addUser/?user=" + $scope.newUserName + "&password=" + gfhdjkagf($scope.newUserPassword))
            .then(function (result) {
                if ((typeof (result.data) == "string") && result.data.indexOf("Success") > -1) {
                    $scope.users.unshift({ Name: $scope.newUserName, Password: $scope.newUserPassword });
                    alert("新建成功");
                } else{
                    alert("新建失败。是否用户名重复？")
                }
            });
    };
    $scope.deleteUser = function () {
        $http.get("/Service/ums/deleteUser/?user=" + $scope.currentUser.Name)
            .then(function (result) {
                if ((typeof (result.data) == "string") && result.data.indexOf("Success") > -1) {
                    for (var index = 0; index < $scope.users.length; index++) {
                        if ($scope.users[index].Name == $scope.currentUser.Name) {
                            $scope.users.splice(index, 1);
                            if (index > 0) {
                                $scope.toggle($scope.users[index]);
                            } else{
                                $scope.toggle($scope.users[0]);
                            }
                        }
                    }
                    $scope.users
                    alert("删除成功");
                }else{
                    alert("删除失败")
                }
            });
    };
    $scope.saveUser = function () {
        $http.get("/Service/ums/saveUser/?user=" + $scope.currentUser.Name + "&password=" + gfhdjkagf($scope.currentUser.Password))
            .then(function (result) {
                if ((typeof (result.data) == "string") && result.data.indexOf("Success") > -1) {
                    alert("修改成功");
                }else{
                    alert("修改失败")
                }
            });
    };
}]);